package com.fagp.mew.cms.vo;

import com.mew.common.entity.back.Admin;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AdminVo implements UserDetails, Serializable {

    private Integer id;

    private String username;

    private String ip;

    private String password;

    private Integer state;

    /**
     * 权限
     */
    Collection<GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.getState()>0;
    }

    public static AdminVo toVo(Admin admin){
        AdminVo adminVo = AdminVo.builder()
                .username(admin.getUsername())
                .password(admin.getPassword())
                .state(admin.getState())
                .build();
        return adminVo;
    }
}
